package com.cy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@SpringBootApplication
public class NacosProviderApplication {

    /**
     * 服务启动时，会自动向nacos发出一个http请求(pom依赖默认本地端口8848，yml更改配置)
     * 将这个服务注册到nacos服务端，nacos服务端会通过一个map来存储这个服务信息
     * 服务信息
     */
    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class,args);
    }

    @Value("${server.port}")
    private String server;

    // localhost:8081/provider/echo/123
    @RestController
    public class ProviderController {
        @GetMapping(value = "/provider/echo/{string}")
        public String doEcho(@PathVariable String string) {
            return server+" Say:Hello Nacos Discovery " + string;
        }
    }

    /**通过此案例演示rest请求的处理*/
    @RestController
    @RequestMapping("/provider/template/")
    public class RestProviderController{

        /**
         * 基于id执行删除操作
         * @param id
         */
        @DeleteMapping("{id}")
        public void doDelete(@PathVariable Integer id){
            System.out.println(id+" is deleted by "+server);
        }

        @PostMapping
        public Map<String,Object> doPost(@RequestBody Map<String,Object> map){
            System.out.println("consumer post data: "+map);
            map.put("status",1);
            map.put("server.port",server);
            return map;
        }

        @PutMapping
        public void doPut(@RequestBody Map<String,Object> map){
            System.out.println("consumer put data: "+map);
        }
    }
}
